package org.aeon.client.context;


import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: MissingAeon
 * @date: 2024/2/21 19:07
 * @description:
 */
@Slf4j
public class MessageMap {
    private final Map<MessageContextKey, Object> map;

    MessageMap() {
        map = new ConcurrentHashMap<>();
    }

    /**
     * 设置
     *
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T set(MessageContextKey key, T value) {
        // log.debug("存入了{}", value);
        final Object ret = map.put(key, value);
        if (ret != null) {
            return (T) ret;
        }
        return null;
    }


    /**
     * 获取
     *
     * @param key
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T get(MessageContextKey key) {
        final Object ret = map.get(key);
        if (ret == null) {
            throw new NullPointerException("不允许的空值");
        }
        // log.debug("获取了{}", ret);
        return (T) ret;
    }

    /**
     * 获取
     *
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T get(MessageContextKey key, Class<T> clazz) {
        final Object ret = map.get(key);
        if (ret == null) {
            throw new NullPointerException("不允许的空值");
        }
        if (clazz.isInstance(ret)) {
            // log.debug("获取了{}", ret);
            return (T) ret;
        }
        throw new NullPointerException("不允许的类型");
    }


    /**
     * 是否存在
     *
     * @param key
     * @return
     */
    public boolean exist(MessageContextKey key) {
        return map.containsKey(key);
    }

    /**
     * 删除
     *
     * @param key
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T del(MessageContextKey key) {
        final Object ret = map.remove(key);
        return (T) ret;
    }

    public enum MessageContextKey {
        LoginMessage,
        RegisterMessage,
        MemberList,
        GroupMapList,
        FriendConfirmationList,
        GroupConfirmationList,
        UserInfo,
        GroupInfo,
        SearchUserList,
        SearchGroupList
    }
}
